package com.iwhalecloud.uncc.dao.impl;

import com.iwhalecloud.uncc.common.ArchiverConstant;
import com.iwhalecloud.uncc.common.ArchiverThreadLocal;
import com.iwhalecloud.uncc.dao.ArchiverDao;
import com.iwhalecloud.uncc.db.DatasourceContextHolder;
import com.iwhalecloud.uncc.domain.ArchiveParam;
import com.iwhalecloud.uncc.model.InsAlarmMsg;
import com.iwhalecloud.uncc.utils.BatchUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author zq
 * @date 2021/10/26 23:30
 * @description
 */
@Repository("insAlarmMsgArchiverDao")
public class InsAlarmMsgArchiverDaoImpl implements ArchiverDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Object query(Object o) {
        return null;
    }

    @Override
    public List queryList(Object o) {
        StringBuilder sql = new StringBuilder("SELECT * FROM INS_ALARM_MSG WHERE COLL_SERVICE_ID = ? AND FINISH_TIME BETWEEN ? AND ? AND ARCHIVER_FLAG = " + ArchiverConstant.ONLINE);
        ArchiveParam aP = ArchiverThreadLocal.getArchiveParam();
        List<InsAlarmMsg> list = jdbcTemplate.query(sql.toString(), new Object[]{o,aP.getStartDate(),aP.getEndDate()} , new BeanPropertyRowMapper<>(InsAlarmMsg.class));
        if (!list.isEmpty()) {
            return list;
        }
        return null;
    }

    @Override
    public List batchQuery(List list) {
        StringBuilder sql = new StringBuilder("SELECT");
        sql.append(" `ALARM_ID`, `SOURCE_ID`, `COLL_SERVICE_ID`, `CREATE_TIME`, `FINISH_TIME`, `INNER_ALARM_ID`, `PARTITION_CODE`");
        sql.append(" FROM INS_ALARM_MSG WHERE COLL_SERVICE_ID in (:i)");
        ArchiveParam aP = ArchiverThreadLocal.getArchiveParam();
        int limit = (list.size())*(aP.getEachInsLines());
        sql.append(" AND FINISH_TIME BETWEEN \"" + aP.getStartDate() + "\" AND \"" + aP.getEndDate()+"\"");
        sql.append(" AND ARCHIVER_FLAG = " + ArchiverConstant.ONLINE);
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        sql.append(" LIMIT " + limit);
        List<InsAlarmMsg> res = BatchUtil.batchQueryBeans(sql,list,new RowMapper() {
            @Override
            public Object mapRow(ResultSet resultSet, int i) throws SQLException {
                InsAlarmMsg insAlarmMsg = new InsAlarmMsg();
                insAlarmMsg.setAlarmId(resultSet.getLong("ALARM_ID"));
                insAlarmMsg.setSourceId(resultSet.getInt("SOURCE_ID"));
                insAlarmMsg.setCollServiceId(resultSet.getInt("COLL_SERVICE_ID"));
                insAlarmMsg.setCreateTime(resultSet.getLong("CREATE_TIME"));
                insAlarmMsg.setFinishTime(resultSet.getLong("FINISH_TIME"));
                insAlarmMsg.setInnerAlarmId(resultSet.getLong("INNERALARM_ID"));
                insAlarmMsg.setPartitionCode(resultSet.getString("PARTITION_CODE"));
                return insAlarmMsg;
            }
        });
        return res;
    }

    @Override
    public void insert(Object o) {

    }

    @Override
    public void batchInsert(List list) {
        StringBuilder sql = new StringBuilder("INSERT INTO `ins_alarm_msg`");
        sql.append(" (`ALARM_ID`, `SOURCE_ID`, `COLL_SERVICE_ID`, `CREATE_TIME`, `FINISH_TIME`, `INNER_ALARM_ID`, `PARTITION_CODE`, `ARCHIVER_TIME`)");
        sql.append(" VALUES (:alarmId,:sourceId,:collServiceId,:createTime,:finishTime,:innerAlarmId,:partitionCode,:archiverTime)");
        BatchUtil.batchUpdateBeans(sql,list);
    }

    @Override
    public void update(int flag, Object o) {

    }

    @Override
    public void batchUpdate(int flag, List list) {
        StringBuilder sql = new StringBuilder("UPDATE INS_ALARM_MSG SET ARCHIVER_FLAG = " + flag + " WHERE ALARM_ID = :alarmId");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        BatchUtil.batchUpdateBeans(sql,list);
    }

    @Override
    public void delete(Object o) {

    }

    @Override
    public void batchDelete(List list) {
        StringBuilder sql = new StringBuilder("DELETE FROM INS_ALARM_MSG WHERE ALARM_ID = :alarmId");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        BatchUtil.batchUpdateBeans(sql,list);
    }
}
